﻿using System;
using System.Collections.Generic;
using System.Text;
using XDesigner.ORM.DOM ;

namespace XDesigner.ORM
{
    public class InstanceEnumerator : System.Collections.IEnumerable , System.Collections.IEnumerator 
    {
        public InstanceEnumerator()
        {

        }

        public InstanceEnumerator(System.Data.IDataReader reader, Type instanceType , ORMEngine engine)
        {
            this._Reader = reader;
            this._InstanceType = instanceType;
            this._Engine = engine;
        }

        private System.Data.IDataReader _Reader = null;

        public System.Data.IDataReader Reader
        {
            get { return _Reader; }
            set { _Reader = value; }
        }

        private ORMEngine _Engine = null;

        public ORMEngine Engine
        {
            get { return _Engine; }
            set { _Engine = value; }
        }

        private Type _InstanceType = null;

        public Type InstanceType
        {
            get { return _InstanceType; }
            set { _InstanceType = value; }
        }
         

        private bool startFlag = false;

        #region IEnumerator 成员

        private object objCurrent = null ;

        public object Current
        {
            get
            {
                if (startFlag)
                {
                    return objCurrent;
                }
                else
                {
                    throw new InvalidOperationException( ORMStrings.EnumeratorNotStarted );
                }
            }
        }

        private ORMTypeMappingHelper helper = null;

        public bool MoveNext()
        {
            if (startFlag == false)
            {
                if (this._Engine == null)
                {
                    throw new ArgumentNullException("Engine");
                }
                if (this._InstanceType == null)
                {
                    throw new ArgumentNullException("InstanceType");
                }
                if (this._Reader == null)
                {
                    throw new ArgumentNullException("Reader");
                }
                helper = this._Engine.MappingHelpers[this._InstanceType];
                if (helper == null)
                {
                    throw new NotSupportedException(this._InstanceType.FullName);
                }
                startFlag = true;
            }//if

            ORMContext context = new ORMContext(this._Engine);
            Array items = this._Engine.InnerReadInstances(
                context,
                this._Reader,
                helper.ORMType,
                null,
                1);

            if (items != null && items.Length == 1)
            {
                objCurrent = items.GetValue(0);
                return true;
            }
            else
            {
                objCurrent = null;
                return false;
            }
        }

        public void Reset()
        {
            throw new NotSupportedException("Reset");
        }

        #endregion

        #region IEnumerable 成员

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this;
        }

        #endregion
    }
}
